﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aij1.Cms;
using Aij1.Utils;
using Aij1.BLL;
using Aij1.Model;
using System.Text;
using System.Data;

namespace Aij1
{
    public partial class Shop : BasePage
    {
        int catId;
        protected WebPager WPager;
        protected IDataReader Reader;
        protected A_Category C;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (catId > 0) C = XpoSession.GetObjectByKey<A_Category>(catId);
            PageName = C == null ? "套系和商品" : C.Title;
            Keywords = "亲子照,儿童照,满月照,韩框,金蚕丝册,动感水晶,琉璃册,全家福,套系预订,爱+1";
            Description = Config.Description;
            Reader = AlbumData.GetPagedAlbumsList(WPager);
        }

        protected void Page_init(object sender, EventArgs e)
        {
            catId = CommOp.ToInt(Request.QueryString["Id"]);
            CNM.CategoryId = catId;

            WPager = new WebPagerSEO()
            {
                KeyId = "Oid",
                OrderBy = "SetTopTime DESC,DisplayTime DESC",
                Query = @"SELECT a.OID,a.Title,a.DisplayTime,a.SetTopTime,a.Intro,s.Price,s.OriginalPrice,p.SnapShotUrl,p.SnapShotWidth,p.SnapShotHeight,c.OID  as CategoryId,c.Title as CategoryTitle FROM 
A_Article a LEFT JOIN A_Photo p ON a.SamplePhotoId=p.OID INNER JOIN A_Category c ON A.CategoryId=c.OID 
INNER JOIN B_Series s ON a.OID=s.OID WHERE a.GCRecord IS NULL AND Status=3 AND " +
String.Format(catId == 0 ? "a.AType={0} AND c.AType={0}" : "a.AType={0} AND CategoryId={1}", (int)ArticleType.商品, catId),
                PageSize = 16,
            };
        }

        public string GetSeriesListHtml(IDataReader reader, int w, int h)
        {
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            {
                sb.Append(GetSeriesHtml(reader, w, h));
            }
            return sb.ToString();
        }

        static string format = @"<div class='series clearfix'>
<div class='titleImg'>
<div style='width:{9}px;height:{10}px; overflow:hidden; float: left;'>
    <a href='ShopDetails_{7}.htm'><img src='{0}' border='0' style='margin-left: {1}px; margin-top: {2}px; width: {3}px;
        height: {4}px;' alt='{5}' /></a></div><a href='ShopDetails_{7}.htm'>{5}</a><br />
    <span class='price'>{6:C}</span><br />
    <input type='button' onclick='Reserve({7},1)' value='现在预订!' />
</div>
<div class='intro'>{8}</div>
</div>";

        static string formatSimple = @"<div class='series clearfix'>
<div style='width:{9}px;height:{10}px; overflow:hidden;'>
   <a href='ShopDetails_{7}.htm'><img src='{0}' border='0' style='margin-left: {1}px; margin-top: {2}px; width: {3}px;
        height: {4}px;' alt='{5}' /></a>
</div><a href='ShopDetails_{7}.htm'>{5}</a><br />
<span class='price'>{6:C}</span><br />
<input type='button' onclick='Reserve({7},1)' value='现在预订!' />
</div>";

        string GetSeriesHtml(IDataReader reader, int divW, int divH)
        {
            int sWidth = CommOp.ToInt(reader["SnapShotWidth"]);
            int sHeight = CommOp.ToInt(reader["SnapShotHeight"]);
            if (sWidth <= 0) sWidth = 120;
            if (sHeight <= 0) sHeight = 120;
            int marginLeft = (divW - sWidth) / 2;
            int marginTop = (divH - sHeight) / 2;
            string intro = CommOp.ToStr(reader["Intro"]);
            string fmt = String.IsNullOrEmpty(intro) ? formatSimple : format;
            string imgUrl = CommOp.ToStr(reader["SnapShotUrl"]);
            if (String.IsNullOrEmpty(imgUrl)) imgUrl = "~/Cms/images/nopic.gif";
            return string.Format(fmt, ResolveClientUrl(imgUrl),
              marginLeft, marginTop, sWidth, sHeight, reader["Title"], reader["Price"], reader["OID"],
              CommOp.ToHtmlStr(intro), divW, divH);
        }

    }
}